﻿Imports DotNetNuke
Imports DotNetNuke.Common
Imports DotNetNuke.Data
Imports DotNetNuke.UI.Utilities.ClientAPI
Imports DotNetNuke.Services.FileSystem
Imports DotNetNuke.Security.Membership
Namespace rsvp


    Public Class view
        Inherits DotNetNuke.Entities.Modules.PortalModuleBase
        Implements DotNetNuke.Entities.Modules.IActionable

        Dim dp As Data.DataProvider
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If DotNetNuke.Framework.AJAX.IsInstalled Then
                DotNetNuke.Framework.AJAX.RegisterScriptManager()
                DotNetNuke.Framework.AJAX.RegisterPostBackControl(bForceRefresh)
            End If

            RegisterScriptBlocks()
            dp = Data.DataProvider.Instance
            If Not Page.IsPostBack Then

                LoadUpcomingEvents()
            End If
        End Sub

        Public ReadOnly Property ModuleActions() As DotNetNuke.Entities.Modules.Actions.ModuleActionCollection Implements DotNetNuke.Entities.Modules.IActionable.ModuleActions
            Get
                Dim actions As New Entities.Modules.Actions.ModuleActionCollection
                'actions.Add(GetNextActionID, "Edit Custom Events", Entities.Modules.Actions.ModuleActionType.EditContent, "Edit Custom Events", "Edit.gif", EditUrl("editEvents"), False, SecurityAccessLevel.Edit, True, False)
                Return actions
            End Get
        End Property
        Private Sub RegisterScriptBlocks()
            RegisterClientScriptBlock(Page, "jquery-1.4.2.min.js", "<script src=""" & ModulePath & "js/jquery-1.4.2.min.js""></script><script>$rsvp = jQuery.noConflict();</script>")
            'UI
            RegisterClientScriptBlock(Page, "jquery.ui.core.min.js", "<script src=""" & ModulePath & "js/jquery.ui.core.min.js""></script>")
            RegisterClientScriptBlock(Page, "jquery.ui.position.min.js", "<script src=""" & ModulePath & "js/jquery.ui.position.min.js""></script>")
            RegisterClientScriptBlock(Page, "jquery.ui.widget.min.js", "<script src=""" & ModulePath & "js/jquery.ui.widget.min.js""></script>")
            RegisterClientScriptBlock(Page, "jquery.ui.mouse.min.js", "<script src=""" & ModulePath & "js/jquery.ui.mouse.min.js""></script>")
            RegisterClientScriptBlock(Page, "jquery.ui.draggable.min.js", "<script src=""" & ModulePath & "js/jquery.ui.draggable.min.js""></script>")
            RegisterClientScriptBlock(Page, "jquery.ui.resizable.min.js", "<script src=""" & ModulePath & "js/jquery.ui.resizable.min.js""></script>")
            RegisterClientScriptBlock(Page, "jquery.ui.datepicker.min.js", "<script src=""" & ModulePath & "js/jquery.ui.datepicker.min.js""></script>")
            RegisterClientScriptBlock(Page, "jquery.ui.button.min.js", "<script src=""" & ModulePath & "js/jquery.ui.button.min.js""></script>")
            RegisterClientScriptBlock(Page, "jquery.ui.dialog.min.js", "<script src=""" & ModulePath & "js/jquery.ui.dialog.min.js""></script>")
            'UI Effects
            RegisterClientScriptBlock(Page, "jquery.effects.core.min.js", "<script src=""" & ModulePath & "js/jquery.effects.core.min.js""></script>")
            RegisterClientScriptBlock(Page, "jquery.effects.highlight.min.js", "<script src=""" & ModulePath & "js/jquery.effects.highlight.min.js""></script>")
            RegisterClientScriptBlock(Page, "jquery.effects.scale.min.js", "<script src=""" & ModulePath & "js/jquery.effects.scale.min.js""></script>")
            RegisterClientScriptBlock(Page, "jquery.effects.transfer.min.js", "<script src=""" & ModulePath & "js/jquery.effects.transfer.min.js""></script>")
            RegisterClientScriptBlock(Page, "jquery.ui.dialog.min.js", "<script src=""" & ModulePath & "js/jquery.ui.dialog.min.js""></script>")
            RegisterClientScriptBlock(Page, "jquery.ui.accordion.min.js", "<script src=""" & ModulePath & "js/jquery.ui.accordion.min.js""></script>")
            RegisterClientScriptBlock(Page, "jquery.jcarousel.min.js", "<script src=""" & ModulePath & "js/jquery.jcarousel.min.js""></script>")

            'RegisterClientScriptBlock(Page, "jquery.bgiframe.min.js", "<script src=""" & ModulePath & "js/jquery.bgiframe.min.js""></script>")

            'CSS
            RegisterClientScriptBlock(Page, "jquery.ui.all.css", "<link type=""text/css"" href=""" & ModulePath & "css/themes/base/jquery.ui.all.css"" rel=""stylesheet"" />")

        End Sub
        Private Sub LoadUpcomingEvents()
            rUpEvents.DataSource = dp.ExecuteDataSet("rsvp_events_get_all", ModuleId).Tables(0)
            rUpEvents.DataBind()
        End Sub

        Private Sub rUpEvents_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rUpEvents.ItemDataBound
            If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
                Dim drv As System.Data.DataRowView = e.Item.DataItem
                If Not drv Is Nothing Then
                    'If Not drv.Item("hostuserid") = UserId.ToString Then
                    '    Dim lEventButtons As Literal = CType(e.Item.FindControl("lEventButtons"), Literal)
                    '    lEventButtons.Text = ""
                    'End If


                    Dim rComments As Repeater = CType(e.Item.FindControl("rComments"), Repeater)
                    If Not rComments Is Nothing Then
                        Dim dtSource As DataTable = dp.ExecuteDataSet("rsvp_comments_get_all", drv.Item("eventid")).Tables(0)
                        Dim dtnew As DataTable = dtSource.Copy()
                        dtnew.Columns.Add("displayname", GetType(String))
                        dtnew.Columns.Add("image", GetType(String))

                        Dim mp As Security.Membership.MembershipProvider = Security.Membership.MembershipProvider.Instance()
                        Dim user As DotNetNuke.Entities.Users.UserInfo

                        For Each dr As DataRow In dtnew.Rows
                            Try
                                user = mp.GetUser(PortalId, Convert.ToInt16(dr("posteruserid").ToString))
                                dr("displayname") = user.DisplayName
                                If user.Profile.ProfileProperties.GetByName("Photo") Is Nothing Then
                                    dr("image") = ModulePath & "images/no_avatar.gif"
                                Else
                                    Dim photoID As Integer = Int(user.Profile.ProfileProperties.GetByName("Photo").PropertyValue)
                                    If photoID = -1 Then
                                        dr("image") = ModulePath & "images/no_avatar.gif"
                                    Else
                                        dr("image") = fileURL(photoID)
                                    End If

                                End If
                            Catch ex As Exception
                                dr("displayname") = "Unknown"
                                dr("image") = ModulePath & "images/no_avatar.gif"
                            End Try
                            

                        Next

                        rComments.DataSource = dtnew
                        rComments.DataBind()
                    End If
                End If
            End If
        End Sub

        Private Sub bEventAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bEventAdd.Click
            Dim StartDate As New DateTime
            Dim EndDate As New DateTime
            If DateTime.TryParse(tbEventStartDate.Text.Trim & " " & ddlEventAddStartTime.SelectedValue, StartDate) Then
                If DateTime.TryParse(tbEventEndDate.Text.Trim & " " & ddlEventAddEndTime.SelectedValue, EndDate) Then
                    dp.ExecuteNonQuery("rsvp_events_add", tbEventAddTitle.Text, tbEventAddDescription.Text, StartDate, EndDate, UserId, ModuleId)
                Else
                    dp.ExecuteNonQuery("rsvp_events_add", tbEventAddTitle.Text, tbEventAddDescription.Text, StartDate, DBNull.Value, UserId, ModuleId)
                End If
            End If


        End Sub

        Private Sub bEventEditLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bEventEditLoad.Click
            Dim StartDate As New DateTime
            Dim EndDate As New DateTime
            Dim dt As DataTable = dp.ExecuteDataSet("rsvp_events_get", New Guid(hfEventEditEventID.Value)).Tables(0)
            For Each dr As DataRow In dt.Rows
                tbEventEditTitle.Text = dr("title")
                tbEventEditDescription.Text = dr("description")
                StartDate = dr("eventstart")
                tbEventEditStartDate.Text = StartDate.ToShortDateString()
                EndDate = dr("eventend")
                tbEventEditEndDate.Text = EndDate.ToShortDateString()
                ddlEventEditStartTime.SelectedValue = StartDate.ToString("HH:mm:ss") 'StartDate.ToShortTimeString
                ddlEventEditEndTime.SelectedValue = EndDate.ToString("HH:mm:ss")
            Next
            Dim sb As New StringBuilder
            With sb
                .Append("<script type=""text/javascript"">" & vbCrLf)
                .Append("  $rsvp(""div[id='divEventEditStartDate']"").datepicker( ""setDate"" , """ & StartDate.ToShortDateString() & """);" & vbCrLf)
                .Append("  $rsvp(""div[id='divEventEditEndDate']"").datepicker( ""setDate"" , """ & EndDate.ToShortDateString() & """);" & vbCrLf)
                .Append("  $rsvp(""div[id='divEventEditAccordion']"").accordion( ""activate"" , 0 );" & vbCrLf)
                .Append("</script>" & vbCrLf)
            End With

            ScriptManager.RegisterStartupScript(Page, Me.GetType, "EditEventLoadScript", sb.ToString, False)
            upEventEditTD.Update()
            upEventEditStart.Update()
            upEventEditEnd.Update()
        End Sub

        Private Sub bEventDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bEventDelete.Click
            dp.ExecuteNonQuery("rsvp_events_delete", New Guid(hfEventDeleteEventID.Value))
        End Sub

        Private Sub bCommentAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bCommentAdd.Click
            dp.ExecuteNonQuery("rsvp_comments_add", New Guid(hfCommentAddEventID.Value), tbCommentAddComment.Text, UserId)
        End Sub

        Private Sub bCommentEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bCommentEdit.Click
            dp.ExecuteNonQuery("rsvp_comments_update", Convert.ToInt32(hfCommentEditCommentID.Value), tbCommentEditComment.Text)
        End Sub

        Private Sub bCommentEditLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bCommentEditLoad.Click
            Dim dt As DataTable = dp.ExecuteDataSet("rsvp_comments_get", Convert.ToInt32(hfCommentEditCommentID.Value)).Tables(0)
            For Each dr As DataRow In dt.Rows
                tbCommentEditComment.Text = dr("comment")
            Next
            upCommentEditComment.Update()
        End Sub

        Private Sub bEventEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bEventEdit.Click
            Dim StartDate As New DateTime
            Dim EndDate As New DateTime
            If DateTime.TryParse(tbEventEditStartDate.Text.Trim & " " & ddlEventEditStartTime.SelectedValue, StartDate) Then
                If DateTime.TryParse(tbEventEditEndDate.Text.Trim & " " & ddlEventEditEndTime.SelectedValue, EndDate) Then
                    dp.ExecuteNonQuery("rsvp_events_update", New Guid(hfEventEditEventID.Value), tbEventEditTitle.Text, tbEventEditDescription.Text, StartDate, EndDate)
                Else
                    dp.ExecuteNonQuery("rsvp_events_update", New Guid(hfEventEditEventID.Value), tbEventEditTitle.Text, tbEventEditDescription.Text, StartDate, DBNull.Value)
                End If
            End If
        End Sub

        Private Sub bCommentDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bCommentDelete.Click
            dp.ExecuteNonQuery("rsvp_comments_delete", Convert.ToInt32(hfCommentDeleteCommentID.Value))
        End Sub

        Private Sub bForceRefresh_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bForceRefresh.Click
            LoadUpcomingEvents()
        End Sub
        Private ReadOnly Property fileURL(ByVal fileid As Integer) As String
            Get
                Dim fileCnt As New FileController
                Dim objFile As FileInfo = fileCnt.GetFileById(fileid, PortalId)
                Return PortalSettings.HomeDirectory & objFile.Folder & objFile.FileName
            End Get
        End Property
    End Class
    
End Namespace